package springobjects;

import framework.Force;
import java.awt.geom.Point2D;
import massobjects.BaseMass;


/**
 * 
 * @author David Howdyshell, Zack Hopping
 */
public class Spring extends BaseConnector {
    // state
    private BaseMass myStart;
    private BaseMass myEnd;
    private double myK;
    private double myLength;

    /**
     * Constructor for spring. Sets start and end point and spring constant.
     * 
     * @param start - mass at start point
     * @param end - mass at end point
     * @param length - rest length of spring
     * @param kVal - spring constant
     */
    public Spring (BaseMass start, BaseMass end, double length, double kVal) {
        super(start, end, length);
        myStart = start;
        myEnd = end;
        myK = kVal;
        myLength = length;
    }

    /**
     * Update method applies force to the mass at start and end point.
     * 
     * @param dt - delta time since last update
     */
    public void update (double dt) {
        Point2D start = myStart.getCenter();
        Point2D end = myEnd.getCenter();
        double dx = start.getX() - end.getX();
        double dy = start.getY() - end.getY();

        // apply hooke's law to each attached mass
        Force f = new Force(Force.angleBetween(dx, dy), myK
                * (myLength - Force.distanceBetween(dx, dy)));
        myStart.applyForce(f);
        f.negate();
        myEnd.applyForce(f);
    }

    /**
     * Getter method for spring constant.
     * 
     * @return myK
     */
    public double getK () {
        return myK;
    }
}
